<?php

namespace app\command\task;
use app\common\model\game\Room;
use think\Exception;
use think\facade\Cache;
use think\facade\Config;
use think\facade\Db;
use think\facade\Log;
use app\common\model\game\Log as GameLogModel;


class AddGameLog
{
    public function handle()
    {
        Log::channel('command')->write('开始写入游戏日志======');
        $this->run();
        Log::channel('command')->write('结束写入游戏日志======');
    }

    public function run(): bool
    {
       $redis = Cache::store('redis');
       $redis->select(0);
       $room_ids = (new Room())->column('room_id');
       if(empty($room_ids)) return true;
       foreach ($room_ids as $room_id){
           $key = "FwGameRecord:".$room_id;
           $data = $redis->lrange($key,0,-1);
           if(!empty($data)){
               $insert_data = [];
               foreach ($data as $datum){
                   $insert_data[] = [
                       'room_id' => $datum['room_id'],
                       'start_coins' => $datum['start_coins'],
                       'end_coins' => $datum['end_coins'],
                       'start_points' => $datum['start_points'],
                       'end_points' => $datum['end_points'],
                       'start_time' => $datum['start_time'],
                       'end_time' => $datum['end_time'],
                   ];
               }
               (new GameLogModel())->strict(false)->replace()->insertAll($insert_data);
               //清空redis信息
               $redis->delete($key);
           }
       }
       return true;
    }



}